/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import Logica.Core.Movimientos.Bascula;
import Logica.Core.Movimientos.Movimiento;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;

/**
 *
 * @author Luciano
 */
public class BasculaJpaController implements Serializable {

    public BasculaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public BasculaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Bascula bascula) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(bascula);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBascula(bascula.getMiMovimiento()) != null) {
                throw new PreexistingEntityException("Bascula " + bascula + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Bascula bascula) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            bascula = em.merge(bascula);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Movimiento id = bascula.getMiMovimiento();
                if (findBascula(id) == null) {
                    throw new NonexistentEntityException("The bascula with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Date id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Bascula bascula;
            try {
                bascula = em.getReference(Bascula.class, id);
                bascula.getMiMovimiento();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The bascula with id " + id + " no longer exists.", enfe);
            }
            em.remove(bascula);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Movimiento id) throws NonexistentEntityException {
        destroy(id.getFechaHora());
    }

    public List<Bascula> findBasculaEntities() {
        return findBasculaEntities(true, -1, -1);
    }

    public List<Bascula> findBasculaEntities(int maxResults, int firstResult) {
        return findBasculaEntities(false, maxResults, firstResult);
    }

    private List<Bascula> findBasculaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Bascula as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Bascula findBascula(Date id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Bascula.class, id);
        } finally {
            em.close();
        }
    }

    public Bascula findBascula(Movimiento id) {
        return findBascula(id.getFechaHora());
    }

    public int getBasculaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Bascula as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
